<?php	namespace Home\Controller;
class GoodsController extends CommonController
{
	public function index()
	{
		$sid = Q('get.sid',0,'intval');
//		p($sid);
		$GoodsOldData = Db::table('goods')->join('details','details_sid','=','sid')->where('sid',$sid)->get();
		foreach ($GoodsOldData as $k => $v) {
			$GoodsOldData[$k]['small'] = explode(',', $v['small']);
			$GoodsOldData[$k]['mean'] = explode(',', $v['mean']);
			$GoodsOldData[$k]['big'] = explode(',', $v['big']);
			View::with('Bigthumb',$GoodsOldData[$k]['big']);
			$GoodsOldData[$k]['attr'] = Db::table('typeattr')->where('class',1)->where('typeattr_lid',$v['typeattr_lid'])->limit(4)->get();
			$GoodsOldData[$k]['spec'] = Db::table('typeattr')->where('class',0)->where('typeattr_lid',$v['typeattr_lid'])->get(); 
			foreach ($GoodsOldData[$k]['attr'] as $ke => $va) {
				$GoodsOldData[$k]['attr'][$ke]['xvalue'] = Db::table('goodsattr')->where('attribute_xid',$va['xid'])->where('goods_sid',$sid)->get();
			}
			foreach ($GoodsOldData[$k]['spec'] as $ke => $va) {
				$GoodsOldData[$k]['spec'][$ke]['xvalue'] = Db::table('goodsattr')->where('attribute_xid',$va['xid'])->where('goods_sid',$sid)->get();
			}
				
		}
//		p($GoodsOldData);
		View::with('GoodsOldData',$GoodsOldData);
		
		/*//位置显示
		$arr = array();
		function run($Cid){
			
		}
		$Cid = Db::table('cate')->where('sid',$sid)->pluck('cate_cid');
		$arr = run($Cid);*/
		View::make();
	}
	public function addajax()
	{
		$goods = explode(',', $_POST['shuzu']);
//		p($goods);
		$id = Q('get.id',0,'intval');
//		p($id);
		if($id==1){
			$data = [ 
		    'id'        => $goods[0], // 商品 ID 
		    'name'      => $goods[1],// 商品名称 
		    'thumb'		=> $goods[2],//小图
		    'options' => array( 'color'     => $goods[3], 'size'  => $goods[4]),// 其他参数如价格、颜色、可以为数组或字符串 
		    'num'       => $goods[5], // 商品数量 
		    'price'     => $goods[6], // 商品价格 
			]; 
			Cart::add($data); // 添加到购物车 
	//		p($_SESSION); 
		}else{
			$data = array(
				'id'        => $goods[0], // 商品 ID 
		   		'name'      => $goods[1],// 商品名称 
		   		'thumb'		=> $goods[2],//小图
		    	'options' => array( 'color'     => $goods[3], 'size'  => $goods[4]),// 其他参数如价格、颜色、可以为数组或字符串 
		    	'num'       => $goods[5], // 商品数量 
		    	'price'     => $goods[6], // 商品价格 
		    	'total'		=>	intval($goods[5])*intval($goods[6]),
			);
			$_SESSION['shop']=$data;
		}
			if($_SESSION){
				echo 1;
			}
//		p($_SESSION); 
		
	}
	public function combine()
	{
//		$_POST['shuzu'];
		$shuzu = rtrim($_POST['shuzu'],',');
//		p($shuzu);
		$shuzu = Db::table('kinds')->where("combine",$shuzu)->pluck('stock');
//		p($shuzu);
		if($shuzu){
			echo $shuzu;
		}else{
			echo 0;
		}
	}
}
